﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SchoolProject2012.Domain;
using SchoolProject2012.Areas.ChargeDePlacement.Models;

namespace SchoolProject2012.Services
{
    public partial class ModelService : IModelService
    {

     
        private Entities model = new Entities();

        #region Entreprise
       
        private Entreprise convertToEntreprise(ENTREPRISE ent){
            Entreprise newEnt = new Entreprise();
            newEnt.id = ent.IDENTREPRISE;
            newEnt.libelle = ent.LIBENTREPRISE;
            newEnt.telephone = ent.telephone;
            newEnt.fax = ent.fax;
            newEnt.mail = ent.mail;
            newEnt.adresse = ent.adresse;
            newEnt.cp = ent.cp;
            newEnt.ville = ent.ville;
            newEnt.infoComp = ent.InfoComplementaire;
            return newEnt;
        }

        private ENTREPRISE convertToModelEntreprise(Entreprise ent){
            ENTREPRISE entreprise = new ENTREPRISE();
            entreprise.IDENTREPRISE = ent.id;
            entreprise.LIBENTREPRISE = ent.libelle;
            entreprise.telephone = ent.telephone;
            entreprise.fax = ent.fax;
            entreprise.mail = ent.mail;
            entreprise.adresse = ent.adresse;
            entreprise.cp = ent.cp;
            entreprise.ville = ent.ville;
            entreprise.InfoComplementaire = ent.infoComp;
            return entreprise;
        }
        
        public Entreprise getEntreprise(int id)
        {
            Entreprise ent = convertToEntreprise(model.ENTREPRISE.Single(e => e.IDENTREPRISE == id));
            ent.contacts = (List<Contactentreprise>)this.getListOfContactEntreprise(ent.id);
            ent.offresDeStage = (List<OffreDeStage>)this.getListOfOffreDeStage(ent.id);
            ent.contrats = (List<Contrat>)this.getListOfContratByEntreprise(ent.id);
            return ent;
        }

        public IEnumerable<Entreprise> getListOfEntreprise()
        {
            IEnumerable<ENTREPRISE> lstEnt = model.ENTREPRISE.ToList();
            List<Entreprise> lstEntToReturn = new List<Entreprise>();
            foreach (ENTREPRISE e in lstEnt) {
                Entreprise ent = convertToEntreprise(e);
                ent.contacts = (List<Contactentreprise>)this.getListOfContactEntreprise(ent.id);
                ent.offresDeStage = (List<OffreDeStage>)this.getListOfOffreDeStage(ent.id);
                ent.contrats = (List<Contrat>)this.getListOfContratByEntreprise(ent.id);
                lstEntToReturn.Add(ent);
            }
            return lstEntToReturn;
        }

        public bool addEntreprise(Entreprise ent)
        {
            ENTREPRISE e = convertToModelEntreprise(ent);
            model.AddToENTREPRISE(e);
            model.SaveChanges();
            return true;
        }

        public bool updateEntreprise(Entreprise ent)
        {
            ENTREPRISE e = convertToModelEntreprise(ent);
            model.ENTREPRISE.Attach(e);
            model.ObjectStateManager.ChangeObjectState(e, EntityState.Modified);
            model.SaveChanges();
            return true;
        }

        public bool deleteEntreprise(int id)
        {
            ENTREPRISE ent = model.ENTREPRISE.Single(e => e.IDENTREPRISE == id);
            model.ENTREPRISE.DeleteObject(ent);
            model.SaveChanges();
            return true;
        }

        #endregion

        #region Contact
        
        private Contactentreprise convertToContactEntreprise(CONTACTENTREPRISE con) {
            Contactentreprise contact = new Contactentreprise();
            contact.nom = con.nom;
            contact.prenom = con.prenom;
            contact.telephone = con.telephone;
            contact.mail = con.mail;
            contact.mobile = con.mobile;
            contact.infoComp = con.infoComplementaire;
            contact.idEntreprise = con.IDENTREPRISE;
            contact.id = con.idContact;
            return contact;
        }

        private CONTACTENTREPRISE convertToModelContactEntreprise(Contactentreprise con) {
            CONTACTENTREPRISE contact = new CONTACTENTREPRISE();
            contact.idContact = con.id;
            contact.nom = con.nom;
            contact.prenom = con.prenom;
            contact.mail = con.mail;
            contact.mobile = con.mobile;
            contact.telephone = con.telephone;
            contact.infoComplementaire = con.infoComp;
            contact.IDENTREPRISE = con.idEntreprise;
            return contact;
        }

        public Contactentreprise getContactEntreprise(int id)
        {
            return convertToContactEntreprise(model.CONTACTENTREPRISE.Single(e => e.idContact == id));
        }

        public IEnumerable<Contactentreprise> getListOfContactEntreprise()
        {
            IEnumerable<CONTACTENTREPRISE> lstCon = model.CONTACTENTREPRISE.ToList();
            List<Contactentreprise> lstConToReturn = new List<Contactentreprise>();
            foreach (CONTACTENTREPRISE e in lstCon)
            {
                lstConToReturn.Add(convertToContactEntreprise(e));
            }
            return lstConToReturn;
        }

        public bool addContactEntreprise(Contactentreprise con)
        {
            CONTACTENTREPRISE e = convertToModelContactEntreprise(con);
            model.AddToCONTACTENTREPRISE(e);
            model.SaveChanges();
            return true;
        }

        public bool updateContactEntreprise(Contactentreprise ent)
        {
            CONTACTENTREPRISE e = convertToModelContactEntreprise(ent);
            model.CONTACTENTREPRISE.Attach(e);
            model.ObjectStateManager.ChangeObjectState(e, EntityState.Modified);
            model.SaveChanges();
            return true;
        }

        public IEnumerable<Contactentreprise> getListOfContactEntreprise(int idEntreprise)
        {
            IEnumerable<CONTACTENTREPRISE> lstCon = model.ENTREPRISE.Single(e => e.IDENTREPRISE == idEntreprise).CONTACTENTREPRISE;
            List<Contactentreprise> lstConToReturn = new List<Contactentreprise>();
            foreach (CONTACTENTREPRISE e in lstCon)
            {
                lstConToReturn.Add(convertToContactEntreprise(e));
            }
            return lstConToReturn;
        }

        public bool deleteContactEntreprise(int id)
        {
            CONTACTENTREPRISE con = model.CONTACTENTREPRISE.Single(e => e.idContact == id);
            model.CONTACTENTREPRISE.DeleteObject(con);
            model.SaveChanges();
            return true;
        }
        #endregion

        #region stage

        private OffreDeStage convertToOffreDeStage(OFFREDESTAGE off)
        {
            OffreDeStage offre = new OffreDeStage();
            offre.id = off.IDOFFREDESTAGE;
            offre.libelle = off.LIBOFFREDESTAGE;
            offre.contenu = off.TEXTE;
            offre.datePublication = off.DATEPUBLICATION.Value;
            offre.dureeStage = off.DUREE;
            offre.remunerationStage = off.REMUNERATION;
            offre.lieuStage = off.LIEUXSTAGE;
            offre.contratPropose = off.CONTRATPROSPOSE;
            offre.idEntreprise = off.IDENTREPRISE;
            return offre;
        }

        private OFFREDESTAGE convertToModelOffreDeStage(OffreDeStage off)
        {
            OFFREDESTAGE offre = new OFFREDESTAGE();
            offre.IDOFFREDESTAGE = off.id;
            offre.LIBOFFREDESTAGE = off.libelle;
            offre.TEXTE = off.contenu;
            offre.DATEPUBLICATION = off.datePublication;
            offre.DUREE = off.dureeStage;
            offre.REMUNERATION = off.remunerationStage;
            offre.LIEUXSTAGE = off.lieuStage;
            offre.CONTRATPROSPOSE = off.contratPropose;
            offre.IDENTREPRISE = off.idEntreprise;
            return offre;
        }

        public OffreDeStage getOffreDeStage(int id)
        {
            return convertToOffreDeStage(model.OFFREDESTAGE.Single(e => e.IDOFFREDESTAGE == id));
        }

        public IEnumerable<OffreDeStage> getListOfOffreDeStage()
        {
            IEnumerable<OFFREDESTAGE> lstOff = model.OFFREDESTAGE.ToList();
            List<OffreDeStage> lstOffToReturn = new List<OffreDeStage>();
            foreach (OFFREDESTAGE o in lstOff)
            {
                lstOffToReturn.Add(convertToOffreDeStage(o));
            }
            return lstOffToReturn;
        }

        public bool addOffreDeStage(OffreDeStage off)
        {
            OFFREDESTAGE o = convertToModelOffreDeStage(off);
            model.AddToOFFREDESTAGE(o);
            model.SaveChanges();
            return true;
        }

        public bool updateOffreDeStage(OffreDeStage off)
        {
            OFFREDESTAGE o = convertToModelOffreDeStage(off);
            model.OFFREDESTAGE.Attach(o);
            model.ObjectStateManager.ChangeObjectState(o, EntityState.Modified);
            model.SaveChanges();
            return true;
        }

        public IEnumerable<OffreDeStage> getListOfOffreDeStage(int idEntreprise)
        {
            IEnumerable<OFFREDESTAGE> lstOff = model.ENTREPRISE.Single(e => e.IDENTREPRISE == idEntreprise).OFFREDESTAGE;
            List<OffreDeStage> lstOffToReturn = new List<OffreDeStage>();
            foreach (OFFREDESTAGE e in lstOff)
            {
                lstOffToReturn.Add(convertToOffreDeStage(e));
            }
            return lstOffToReturn;
        }

        public bool deleteOffreDeStage(int id)
        {
            OFFREDESTAGE off = model.OFFREDESTAGE.Single(e => e.IDOFFREDESTAGE == id);
            model.OFFREDESTAGE.DeleteObject(off);
            model.SaveChanges();
            return true;
        }
        #endregion

        #region eleve

        private Eleve convertToEleve(ELEVE ele)
        {
            Eleve eleve = new Eleve();
            eleve.login = ele.LOGINPERS;
            eleve.mail = ele.MAILPERS;
            eleve.mdp = ele.MDPPERS;
            eleve.nom = ele.NOMPERS;
            eleve.prenom = ele.PRENOMPERS;
            eleve.telephone = ele.NUMTELPERS;
            eleve.adresse = ele.ADRESSEPERS;
            eleve.ville = ele.VILLEPERS;
            eleve.codePostal = ele.CPPERS;
            eleve.dateNaiss = ele.DATENAISSPERS.Value;
            eleve.idChargeP = ele.CHA_LOGINPERS;
            return eleve;
        }

        private ELEVE convertToModelEleve(Eleve eleve)
        {
            ELEVE ele = new ELEVE();
            ele.LOGINPERS = eleve.login;
            ele.MAILPERS = eleve.mail;
            ele.MDPPERS = eleve.mdp;
            ele.NOMPERS = eleve.nom;
            ele.PRENOMPERS = eleve.prenom;
            ele.NUMTELPERS = eleve.telephone;
            ele.ADRESSEPERS = eleve.adresse;
            ele.VILLEPERS = eleve.ville;
            ele.CPPERS = eleve.codePostal;
            ele.DATENAISSPERS = eleve.dateNaiss;
            ele.CHA_LOGINPERS = eleve.idChargeP;
            return ele;
        }

        public Eleve getEleve(string login)
        {
            Eleve eleve = convertToEleve(model.ELEVE.Single(e => e.LOGINPERS == login));
            eleve.contrats = (List<Contrat>)this.getListOfContratByPers(eleve.login);
            return eleve;
        }

        public IEnumerable<Eleve> getListOfEleve()
        {
            IEnumerable<ELEVE> lstEle = model.ELEVE.ToList();
            List<Eleve> lstEleToReturn = new List<Eleve>();
            foreach (ELEVE e in lstEle)
            {
                Eleve eleve = convertToEleve(e);
                eleve.contrats = (List<Contrat>)this.getListOfContratByPers(eleve.login); eleve.contrats = (List<Contrat>)this.getListOfContratByPers(eleve.login);
                lstEleToReturn.Add(eleve);
            }
            return lstEleToReturn;
        }
        
        public bool addEleve(Eleve ele)
        {
            ELEVE e = convertToModelEleve(ele);
            model.AddToELEVE(e);
            model.SaveChanges();
            return true;
        }

        public bool updateEleve(Eleve ele)
        {
            ELEVE e = convertToModelEleve(ele);
            model.ELEVE.Attach(e);
            model.ObjectStateManager.ChangeObjectState(e, EntityState.Modified);
            model.SaveChanges();
            return true;
        }

        public IEnumerable<Eleve> getListOfEleve(int idClasse)
        {
            IEnumerable<ELEVE> lstEle = model.CLASSE.Single(e => e.IDCLASSE == idClasse).ELEVE;
            List<Eleve> lstEleToReturn = new List<Eleve>();
            foreach (ELEVE e in lstEle)
            {
                Eleve eleve = convertToEleve(e);
                eleve.contrats = (List<Contrat>)this.getListOfContratByPers(eleve.login); eleve.contrats = (List<Contrat>)this.getListOfContratByPers(eleve.login);
                lstEleToReturn.Add(eleve);
            }
            return lstEleToReturn;
        }
        
        public bool deleteEleve(string login)
        {
             ELEVE ele = model.ELEVE.Single(e => e.LOGINPERS == login);
            model.ELEVE.DeleteObject(ele);
            model.SaveChanges();
            return true;
        }

        #endregion

        #region classe

        private Classe convertToClasse(CLASSE cla)
        {
            Classe classe = new Classe();
            classe.id = cla.IDCLASSE;
            classe.libelle = cla.LIBCLASSE;
            classe.annee = cla.ANNEECLASSE;
            return classe;
        }

        private CLASSE convertToModelClasse(Classe classe)
        {
            CLASSE cla = new CLASSE();
            cla.IDCLASSE = classe.id;
            cla.LIBCLASSE = classe.libelle;
            cla.ANNEECLASSE = classe.annee;
            return cla;
        }

        public Classe getClasse(int id)
        {
            Classe classe = convertToClasse(model.CLASSE.Single(e => e.IDCLASSE == id));
            classe.lstEleve = (List<Eleve>)this.getListOfEleve(classe.id);
            return classe;
        }

        public IEnumerable<Classe> getListOfClasse()
        {
            IEnumerable<CLASSE> lstCla = model.CLASSE.ToList();
            List<Classe> lstCLaToReturn = new List<Classe>();
            foreach (CLASSE e in lstCla)
            {
                lstCLaToReturn.Add(convertToClasse(e));
            }
            return lstCLaToReturn;
        }

        public bool addClasse(Classe cla)
        {
            CLASSE e = convertToModelClasse(cla);
            model.AddToCLASSE(e);
            model.SaveChanges();
            return true;
        }

        public bool updateClasse(Classe cla)
        {
            CLASSE e = convertToModelClasse(cla);
            model.CLASSE.Attach(e);
            model.ObjectStateManager.ChangeObjectState(e, EntityState.Modified);
            model.SaveChanges();
            return true;
        }

        public bool deleteClasse(int id)
        {
            CLASSE cla = model.CLASSE.Single(e => e.IDCLASSE == id);
            model.CLASSE.DeleteObject(cla);
            model.SaveChanges();
            return true;
        }

        #endregion

        #region contrat

        private Contrat convertToContrat(CONTRAT con)
        {
            Contrat contrat = new Contrat();
            contrat.idEntreprise = con.IDENTREPRISE;
            contrat.idEleve = con.LOGINPERS;
            contrat.typeContrat = con.TYPECONTRAT;
            contrat.AnneeContrat = con.ANNEECONTRAT;
            contrat.dteDbt = con.DteDbt.Value;
            contrat.dteFin = con.DteFin.Value;
            contrat.montantGratification = con.montantGratification.Value;
            return contrat;
        }

        private CONTRAT convertToModelContrat(Contrat contrat)
        {
            CONTRAT con = new CONTRAT();
            con.IDENTREPRISE = contrat.idEntreprise;
            con.LOGINPERS = contrat.idEleve;
            con.TYPECONTRAT = contrat.typeContrat;
            con.ANNEECONTRAT = contrat.AnneeContrat;
            con.DteDbt = contrat.dteDbt;
            con.DteFin = contrat.dteFin;
            con.montantGratification = contrat.montantGratification;
            return con;
        }

        public Contrat getContrat(string loginPers, string annee)
        {
            return convertToContrat(model.CONTRAT.Where(e => e.LOGINPERS == loginPers).Where(e => e.ANNEECONTRAT == annee).Single());
        }

        public IEnumerable<Contrat> getListOfContrat()
        {
            IEnumerable<CONTRAT> lstCon = model.CONTRAT.ToList();
            List<Contrat> lstConToReturn = new List<Contrat>();
            foreach (CONTRAT e in lstCon)
            {
                lstConToReturn.Add(convertToContrat(e));
            }
            return lstConToReturn;
        }

        public IEnumerable<Contrat> getListOfContratByYear(string annee)
        {
            IEnumerable<CONTRAT> lstCon = model.CONTRAT.Where(e => e.ANNEECONTRAT == annee).ToList();
            List<Contrat> lstConToReturn = new List<Contrat>();
            foreach (CONTRAT e in lstCon)
            {
                lstConToReturn.Add(convertToContrat(e));
            }
            return lstConToReturn;
        }

        public IEnumerable<Contrat> getListOfContratByPers(string loginPers)
        {
            IEnumerable<CONTRAT> lstCon = model.CONTRAT.Where(e => e.LOGINPERS == loginPers).ToList();
            List<Contrat> lstConToReturn = new List<Contrat>();
            foreach (CONTRAT e in lstCon)
            {
                lstConToReturn.Add(convertToContrat(e));
            }
            return lstConToReturn;
        }

        public IEnumerable<Contrat> getListOfContratByEntreprise(int idEntreprise)
        {
            IEnumerable<CONTRAT> lstCon = model.CONTRAT.Where(e => e.IDENTREPRISE == idEntreprise).ToList();
            List<Contrat> lstConToReturn = new List<Contrat>();
            foreach (CONTRAT e in lstCon)
            {
                lstConToReturn.Add(convertToContrat(e));
            }
            return lstConToReturn;
        }

        public bool addContrat(Contrat con)
        {
            CONTRAT e = convertToModelContrat(con);
            model.AddToCONTRAT(e);
            model.SaveChanges();
            return true;
        }

        public bool updateContrat(Contrat con)
        {
            CONTRAT c = convertToModelContrat(con);
            model.CONTRAT.Attach(c);
            model.ObjectStateManager.ChangeObjectState(c, EntityState.Modified);
            model.SaveChanges();
            return true;
        }

        public bool deleteContrat(string login, int idEntreprise, string annee)
        {
            CONTRAT con = model.CONTRAT.Where(x => x.LOGINPERS == login).Where(x => x.IDENTREPRISE == idEntreprise).Where(x => x.ANNEECONTRAT == annee).Single();
            model.CONTRAT.DeleteObject(con);
            model.SaveChanges();
            return true;
        }

        #endregion

        #region chargeDePlacement

        private ChargeDePlacement convertToChargeDePlacement(CHARGEDEPLACEMENT cp)
        {
            ChargeDePlacement chargep = new ChargeDePlacement();
            chargep.login = cp.LOGINPERS;
            chargep.mail = cp.MAILPERS;
            chargep.mdp = cp.MDPPERS;
            chargep.nom = cp.NOMPERS;
            chargep.prenom = cp.PRENOMPERS;
            chargep.telephone = cp.NUMTELPERS;
            chargep.adresse = cp.ADRESSEPERS;
            chargep.ville = cp.VILLEPERS;
            chargep.codePostal = cp.CPPERS;
            chargep.dateNaiss = cp.DATENAISSPERS.Value;
            return chargep;
        }

        private CHARGEDEPLACEMENT convertToModelChargeDePlacement(ChargeDePlacement chargep)
        {
            CHARGEDEPLACEMENT cp = new CHARGEDEPLACEMENT();
            cp.LOGINPERS = chargep.login;
            cp.MAILPERS = chargep.mail;
            cp.MDPPERS = chargep.mdp;
            cp.NOMPERS = chargep.nom;
            cp.PRENOMPERS = chargep.prenom;
            cp.NUMTELPERS = chargep.telephone;
            cp.ADRESSEPERS = chargep.adresse;
            cp.VILLEPERS = chargep.ville;
            cp.CPPERS = chargep.codePostal;
            cp.DATENAISSPERS = chargep.dateNaiss;
            return cp;
        }

        public IEnumerable<Eleve> getListOfEleveOfChargeDePlacement(string login)
        {
            IEnumerable<ELEVE> lstEle = model.ELEVE.Where(x => x.CHA_LOGINPERS == login).ToList();
            List<Eleve> lstEleToReturn = new List<Eleve>();
            foreach (ELEVE e in lstEle)
            {
                lstEleToReturn.Add(convertToEleve(e));
            }
            return lstEleToReturn;
        }

        public bool addChargeDePlacement(ChargeDePlacement cp)
        {
            CHARGEDEPLACEMENT c = convertToModelChargeDePlacement(cp);
            model.AddToCHARGEDEPLACEMENT(c);
            model.SaveChanges();
            return true;
        }

        public bool updateChargeDePlacement(ChargeDePlacement cp)
        {
            CHARGEDEPLACEMENT c = convertToModelChargeDePlacement(cp);
            model.CHARGEDEPLACEMENT.Attach(c);
            model.ObjectStateManager.ChangeObjectState(c, EntityState.Modified);
            model.SaveChanges();
            return true;
        }

        public bool deleteChargeDePlacement(string login)
        {
            CHARGEDEPLACEMENT cp = model.CHARGEDEPLACEMENT.Single(x => x.LOGINPERS == login);
            model.CHARGEDEPLACEMENT.DeleteObject(cp);
            model.SaveChanges();
            return true;
        }

        #endregion
    }

}